<?php

namespace app\admin\model\groupon\goods;

use think\Model;
use traits\model\SoftDelete;
use fast\Tree;
use app\admin\model\groupon\activity\Activity;

class Goods extends Model
{

    use SoftDelete;

    

    // 表名
    protected $name = 'groupon_goods';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'activity_status',
        'type_text',
        'status_text',
        'dispatch_type_text',
        'service_ids_arr',
        'images_arr',
        'limit_rules_arr',
        'expire_date',
        'activity_date',
    ];


    // 未開始和进行中的活动
    public function scopeActivity() {
        return $this->where('activity_endtime', '>', time());
    }

    public function getTypeList()
    {
        return ['normal' => __('Type normal'), 'virtual' => __('Type virtual')];
    }

    public function getStatusList()
    {
        return ['up' => __('Status up'), 'hidden' => __('Status hidden'), 'down' => __('Status down')];
    }

    public function getDispatchTypeList()
    {
        return ['selfetch' => __('Dispatch_type selfetch')];
    }

    public function getTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
        $list = $this->getTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getExpireDateAttr($value, $data) {
        return ($this->producttime_text ? $this->producttime_text . ' - ' : '') . ($this->expiretime_text ? $this->expiretime_text : '');
    }

    public function getProducttimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['producttime']) && $data['producttime'] ? $data['producttime'] : '');
        return is_numeric($value) && $value  ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getExpiretimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['expiretime']) && $data['expiretime'] ? $data['expiretime'] : '');
        return is_numeric($value) && $value  ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setProducttimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setExpiretimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    public function getActivityDateAttr($value, $data) {
        return ($this->activity_starttime_text ? $this->activity_starttime_text . ' - ' : '') . ($this->activity_endtime_text ? $this->activity_endtime_text : '');
    }

    public function getActivityStarttimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['activity_starttime']) && $data['activity_starttime'] ? $data['activity_starttime'] : '');
        return is_numeric($value) && $value  ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getActivityEndtimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['activity_endtime']) && $data['activity_endtime'] ? $data['activity_endtime'] : '');
        return is_numeric($value) && $value  ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setActivityStarttimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setActivityEndtimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    public function getActivityStatusAttr($value, $data)
    {
        $activity_status = null;
        if (isset($data['activity_starttime'])) {
            if ($data['activity_starttime'] > time()) {
                $activity_status = 'waiting';
            } else if ($data['activity_endtime'] && $data['activity_endtime'] < time()) {
                $activity_status = 'ended';
            } else if ($data['activity_starttime'] < time() && $data['activity_endtime'] > time()) {
                $activity_status = 'ing';
            }
        }
        return $activity_status;
    }

    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getDispatchTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['dispatch_type']) ? $data['dispatch_type'] : '');
        $valueArr = explode(',', $value);
        $list = $this->getDispatchTypeList();
        return implode(',', array_intersect_key($list, array_flip($valueArr)));
    }

    public function getCategoryIdsArrAttr($value, $data)
    {
        $arr = $data['category_ids'] ? explode(',', $data['category_ids']) : [];

        $category_ids_arr = [];
        if ($arr) {
            $tree = Tree::instance();
            $tree->init(collection(\app\admin\model\groupon\Category::order('weigh desc,id desc')->select())->toArray(), 'pid');

            foreach ($arr as $key => $id) {
                $category_ids_arr[] = $tree->getParentsIds($id, true);
            }
        }

        return $category_ids_arr;
    }

    public function getServiceIdsArrAttr($value, $data)
    {
        return (isset($data['service_ids']) && $data['service_ids']) ? array_values(array_filter(array_map("intval", explode(',', $data['service_ids'])))) : [];
    }


    public function getImagesArrAttr($value, $data)
    {
        $imagesArray = [];
        if (!empty($data['images'])) {
            $imagesArray = explode(',', $data['images']);
            return $imagesArray;
        }
        return $imagesArray;
    }

    public function getParamsArrAttr($value, $data)
    {
        return (isset($data['params']) && $data['params']) ? json_decode($data['params'], true) : [];
    }


    public function getLimitRulesArrAttr($value, $data)
    {
        return (isset($data['limit_rules']) && $data['limit_rules']) ? json_decode($data['limit_rules'], true) : [];
    }

    protected function setLimitRulesAttr($value)
    {
        return $value && is_array($value) ? json_encode($value) : $value;
    }
}
